package com.instalinks.android.apps.smspostservice;

import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;

public class SettingsActivity extends AppTabBaseActivity {

	private CheckBox chkWebSms;
	private CheckBox chkAlphanumericSender;
	private CheckBox chkShortcodeSender;
	private CheckBox chkAutoReplySMS;
	private CheckBox chkMarkAsRead;
	private CheckBox chkAutoStartOnBoot;
	private CheckBox chkNotificationSound;
	
	private Button btnResetCounter;

	protected void doInitialize() {
		mAppCfg.rePopulate();

		setContentView(R.layout.settings);
		
		btnResetCounter = (Button) this.findViewById(R.id.btnResetCounter);
		this.btnResetCounter.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				mAppCfg.resetCounters();
			}
		});

		//***********************************************************************//
		chkAutoReplySMS = (CheckBox) findViewById(R.id.st_chkAutoReplySMS);
		chkMarkAsRead = (CheckBox) findViewById(R.id.st_chkMarkAsRead);
		chkWebSms = (CheckBox) findViewById(R.id.st_chkWebSms);
		chkAlphanumericSender = (CheckBox) findViewById(R.id.st_chkAlphanumericSender);
		chkShortcodeSender= (CheckBox) findViewById(R.id.st_chkShortcodeSender);
		chkAutoStartOnBoot = (CheckBox)findViewById(R.id.st_chkAutoStartOnBoot);
		chkNotificationSound = (CheckBox)findViewById(R.id.st_chkNotificationSound);

		chkAutoReplySMS.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				try {
					mAppCfg.setAutoReplyFlag(isChecked);
				} catch (Exception e) {
				}
			}
		});

		chkMarkAsRead.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				try {
					mAppCfg.setMarkAsReadFlag(isChecked);
				} catch (Exception e) {
				}
			}
		});

		chkAlphanumericSender.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				try {
					mAppCfg.setAlphanumericSenderFlag(isChecked);
				} catch (Exception e) {
				}
				
				chkShortcodeSender.setChecked(isChecked);
				chkShortcodeSender.setEnabled(!isChecked);
			}
		});
		chkShortcodeSender.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				try {
					mAppCfg.setShortcodeSenderFlag(isChecked);
				} catch (Exception e) {
				}
			}
		});
		chkWebSms.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				try {
					mAppCfg.setWebSmsFlag(isChecked);
				} catch (Exception e) {
				}
			}
		});

	
		chkAutoStartOnBoot.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				try {
					mAppCfg.setAutoStartOnBootFlag(isChecked);
				} catch (Exception e) {
				}
			}
		});

		chkNotificationSound.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				try {
					mAppCfg.setNotificationSoundFlag(isChecked);
				} catch (Exception e) {
				}
			}
		});

	}
	
	protected void doPostInitialize(){
		chkAutoReplySMS.setChecked(mAppCfg.isAutoReplyFlag());
		chkMarkAsRead.setChecked(mAppCfg.isMarkAsReadFlag());
		chkWebSms.setChecked(mAppCfg.isWebSmsFlag());
		chkAlphanumericSender.setChecked(mAppCfg.isAlphanumericSenderFlag());
		chkShortcodeSender.setChecked(mAppCfg.isAlphanumericSenderFlag() || mAppCfg.isShortcodeSenderFlag());
		chkShortcodeSender.setEnabled(!mAppCfg.isAlphanumericSenderFlag());
		chkAutoStartOnBoot.setChecked(mAppCfg.isAutoStartOnBootFlag());
		chkNotificationSound.setChecked(mAppCfg.isNotificationSoundFlag());

	}

	@Override
	protected boolean doBackgroundWork() {
//		mAppCfg.registerForC2dm(true);
		return true;
	}

	@Override
	protected void doOnSuccess() {
		// TODO Auto-generated method stub
	}

	@Override
	protected void doOnFailure() {
		// TODO Auto-generated method stub
	}
	
}
